﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SSN.WebFramework.Controls
{
    public enum YesNoEnum
    {
        Yes,
        No,
        Undecided
    }

    public static class YesNoEnumExtensions
    {
        /// <summary>
        /// Converts a nullable boolean value to a YesNoEnum value.
        /// </summary>
        /// <param name="boolean"></param>
        /// <returns></returns>
        public static YesNoEnum ToYesNoEnum(this bool? boolean)
        {
            if (boolean == null || !boolean.HasValue)
            {
                return YesNoEnum.Undecided;
            }

            return boolean.Value ? YesNoEnum.Yes : YesNoEnum.No;
        }

        /// <summary>
        /// Returns a boolean value of a YesNoEnum
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static bool? ToBoolean(this YesNoEnum enumValue)
        {
            switch (enumValue)
            {
                case YesNoEnum.Yes:
                    return true;

                case YesNoEnum.No:
                    return false;

                default:
                    return null;
            }
        }

        /// <summary>
        /// Converts a string value to a YesNoEnum value.
        /// </summary>
        /// <param name="boolean"></param>
        /// <returns></returns>
        public static YesNoEnum ToYesNoEnum(this string stringValue)
        {
            if (string.IsNullOrEmpty(stringValue))
            {
                return YesNoEnum.Undecided;
            }

            stringValue = stringValue.ToUpper();
            if (stringValue == "Y" || stringValue == "YES")
            {
                return YesNoEnum.Yes;
            }

            else if (stringValue == "N" || stringValue == "NO")
            {
                return YesNoEnum.No;
            }

            return YesNoEnum.Undecided;
        }

        /// <summary>
        /// Returns a "yes" for Yes, "no" for No and null for Undecided.
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string ToYesNoString(this YesNoEnum enumValue)
        {
            switch (enumValue)
            {
                case YesNoEnum.Yes:
                    return "yes";

                case YesNoEnum.No:
                    return "no";

                default:
                    return null;
            }
        }

        /// <summary>
        /// Returns a "y" for Yes, "n" for No and null for Undecided.
        /// </summary>
        /// <param name="enumValue"></param>
        /// <returns></returns>
        public static string ToYNString(this YesNoEnum enumValue)
        {
            switch (enumValue)
            {
                case YesNoEnum.Yes:
                    return "Y";

                case YesNoEnum.No:
                    return "N";

                default:
                    return null;
            }
        }
    }
}
